package utils

import (
	"io"
	"os"
	"path/filepath"
)

func Exist(path string) bool  {
	_, err := os.Stat(path)
	return err == nil || os.IsExist(err)
}

func IsDir(path string) bool {
	fInfo, err := os.Stat(path)
	if err != nil {
		return false
	}
	return fInfo.IsDir()
}

func IsFile(path string) bool {
	fInfo, err := os.Stat(path)
	if err != nil {
		return false
	}
	return !fInfo.IsDir()
}

func CopyFile(src, dst string) (bool, error) {
	inFile, err := os.Open(src)
	if err != nil {
		return false, err
	}
	defer inFile.Close()

	var dstPath string
	if IsDir(dst) {
		dstPath = filepath.Join(dst, filepath.Base(src))
	} else {
		dstPath = dst
	}

	outFile, err := os.Create(dstPath)
	if err != nil {
		return false, err
	}
	defer outFile.Close()

	_, err = io.Copy(inFile, outFile)
	if err != nil {
		return false, err
	}
	return true, nil
}

func AbsPath(path string) string {
	pathAbs, _ := filepath.Abs(path)
	return pathAbs
}
